Skip to content

problem 126#2

Open
yuewang0319 wants to merge 1 commit intomasterfrom
126
Open

problem 126#2
yuewang0319 wants to merge 1 commit intomasterfrom
126

Conversation

@yuewang0319
Copy link
Copy Markdown
Owner

No description provided.

@yuewang0319 yuewang0319 self-assigned this Apr 25, 2017
@yuewang0319 yuewang0319 requested a review from tianheng April 25, 2017 06:20
Copy link
Copy Markdown
Collaborator

@tianheng tianheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address those comments


public class Solution {

public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole function is too long. Try to break it down to a few pieces

return wraplist;
}

public void dfs(Map<String, Integer> map, Map<String, Set<String>> neighbors, List<String> sublist, List<List<String>> wraplist, String curWord, String endWord){
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename dfs to something meaningful.

public class Solution {

public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
Map<String, Integer> map = new HashMap<>();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the name map here is confusing. Actually this is a map that records the shortest distance from begin word.

int len = queue.size();
for(int i = 0; i < len; i++){
String curWord = queue.poll();
if(map.get(curWord) == -1 || map.get(curWord) > distance){
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition can be simplified. Think about if such scenario exists: you visit a node, and the node has been visited and it has a larger distance than current distance. Is that possible?

map.put(curWord, distance);
}
if(curWord.equals(endWord)){
found = true;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The found flag is only used to break the loop. Why don't you break here?

}
String newWord = new String(arr);
if(wordSet.contains(newWord)){
if(map.get(newWord) == -1){
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a duplicated check.

map.put(word, -1);
neighbors.put(word, new HashSet<>());
}
int distance = 0;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest to rename it to cur_distance

Set<String> wordSet = new HashSet<>();
for(String word : wordList){
wordSet.add(word);
map.put(word, -1);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: map.put(word, cur_distance - 1);

queue.offer(beginWord);
while(!queue.isEmpty()){
int len = queue.size();
for(int i = 0; i < len; i++){
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not need this for loop. This for loop does nothing good but complex your logic.
While() {
// Only process one node each time
// Pop element from queue
// Update distance and find neighbors.
// Push neighbors into queue.
}

return wraplist;
}

public void dfs(Map<String, Integer> map, Map<String, Set<String>> neighbors, List<String> sublist, List<List<String>> wraplist, String curWord, String endWord){
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So a very good question here, if you can answer this, then you probably know why your program running slow.

What is the difference between

  • DFS from begin to end
  • DFS from end to begin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants